﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach.Book
{
    public partial class FrmAuthorDetail : DevComponents.DotNetBar.Office2007Form
    {
        AuthorBUS authorBUS;

        public ActionName Action { get; set; }
        public AuthorDTO AuthorDto { get; set; }

        public FrmAuthorDetail()
        {
            InitializeComponent();
            authorBUS = new AuthorBUS();
            btnUpdate.Location = btnAdd.Location;
        }

        private void AuthorDetail_Load(object sender, EventArgs e)
        {
            if (Action == ActionName.Insert)
            {
                DocSequenceBUS docSeqBUS = new DocSequenceBUS();
                txtAuthorCode.Text = docSeqBUS.GetNextDocSequenceNumber(DocSequence.Author.ToString());
                txtAuthorName.Focus();
                AuthorDto = new AuthorDTO();
            }
            else
            {
                txtAuthorCode.Text = AuthorDto.AuthorCode;
                txtAuthorName.Text = AuthorDto.AuthorName;
            }

            ProcessButton();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AuthorDto.AuthorCode = txtAuthorCode.Text;
            AuthorDto.AuthorName = txtAuthorName.Text;

            if (!CheckDataInput())
            {
                return;
            }

            AuthorDto.AuthorID = authorBUS.InsertAuthor(AuthorDto);
            if (AuthorDto.AuthorID > 0)
            {
                Global.SetMessage(lblMessage, "Thêm thành công!", true);
                Action = ActionName.Update;
                ProcessButton();

                Book.FrmAuthorList frmList = (Book.FrmAuthorList)Application.OpenForms["FrmAuthorList"];
                if (frmList != null)
                {
                    frmList.AuthorChangeAfterInsert(AuthorDto);
                }
                //OnCategoryChanged(Category);
            }
            else
            {
                Global.SetMessage(lblMessage, "Thêm không thành công!", false);
            }
        }

        private bool CheckDataInput()
        {
            if (string.IsNullOrEmpty(txtAuthorCode.Text) || string.IsNullOrEmpty(txtAuthorName.Text))
            {
                Global.SetMessage(lblMessage, "Tên tác giả không được trống!", false);
                return false;
            }
            return true;
        }

        public void ResetForm()
        {
            DocSequenceBUS docSeqBUS = new DocSequenceBUS();
            txtAuthorName.Text = string.Empty;
            txtAuthorCode.Text = docSeqBUS.GetNextDocSequenceNumber(DocSequence.Author.ToString());
            txtAuthorName.Focus();
        }

        private void ProcessButton()
        {
            if (Action == ActionName.Insert)
            {
                btnUpdate.Visible = false;
                btnAdd.Visible = true;
            }
            else if (Action == ActionName.Update)
            {
                btnUpdate.Visible = true;
                btnAdd.Visible = false;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
            {
                return;
            }

            AuthorDto.AuthorName = txtAuthorName.Text;

            if (authorBUS.UpdateAuthor(AuthorDto))
            {
                Global.SetMessage(lblMessage, "Cập nhật thành công!", true);
                //OnCategoryChanged(Category);
                Book.FrmAuthorList frmList = (Book.FrmAuthorList)Application.OpenForms["FrmAuthorList"];
                if (frmList != null)
                {
                    frmList.AuthorChanged(AuthorDto);
                }
            }
            else
            {
                Global.SetMessage(lblMessage, "Cập nhật không thành công!", false);
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            ResetForm();
            Action = ActionName.Insert;
            ProcessButton();
            AuthorDto = new AuthorDTO();
        }

        private void FrmAuthorDetail_KeyDown(object sender, KeyEventArgs e)
        {
           if (e.KeyCode == Keys.Enter)
            {
                switch (Action)
                {
                    case ActionName.Insert:
                        btnAdd_Click(sender, e);
                        break;
                    case ActionName.Update:
                        btnUpdate_Click(sender, e);
                        break;
                }
            }
        }
    }
}
